<?php
    define('ROUTE_PREFIX', 'api');
    define('CONTROLLER_PATH', '../../controllers');
    define('CONTROLLER_POSTFIX', 'Controller');
    define('CONTROLLER_DEFAULT_METHOD', 'Index');

    class Router {

        public function InitRoutes(){
            include_once '../../libs/epiphany/src/Epi.php';

            Epi::setSetting('exceptions', true);

            Epi::init('route');

            getRoute()->get('/' . ROUTE_PREFIX . '/(\w+)', array('Router', 'InitController_Index'));
            getRoute()->get('/' . ROUTE_PREFIX . '/(\w+)/(\w+)', array('Router', 'InitController_Method'));
            getRoute()->get('/' . ROUTE_PREFIX . '/(\w+)/(\w+)/(\w+)', array('Router', 'InitController_Method_Id'));

            getRoute()->run();
        }

        public static function InitController_Index($controller)
        {
            self::InitController_Method($controller, CONTROLLER_DEFAULT_METHOD);
        }

        public static function InitController_Method($controller, $method)
        {
            self::IncludeFile($controller);

            $controllerClass =  self::GenerateControllerName($controller);

             self::CheckMethod($controllerClass, $method);

            $controllerInstance = new $controllerClass;

            $result = $controllerInstance->$method();

             self::Json($result);
        }

        public static function InitController_Method_Id($controller, $method, $param)
        {
            self::IncludeFile($controller);

            $controllerClass =  self::GenerateControllerName($controller);

            self::CheckMethod($controllerClass, $method);

            $controllerInstance = new $controllerClass;

            $result = $controllerInstance->$method($param);

            self::Json($result);
        }

        private static function IncludeFile($controller)
        {
            $controllerName = self::GenerateControllerName($controller);

            $filePath = CONTROLLER_PATH . '/' . ucfirst($controllerName) . '.php';

             if(!file_exists($filePath)){
                throw new Exception('File ['. $filePath .'] does not exist');
                die();
             }

             include_once $filePath;

             if(!class_exists($controllerName)){
                 throw new Exception('Controller ['. $controllerName .'] does not exist');
                 die();
             }
        }

        private static function CheckMethod($className, $methodName){
            if(!method_exists($className, $methodName)){
                 throw new Exception('Method [' . $methodName . '] does not exists in Controller ['. $className .'].');
                 die();
            }
        }

        private static function GenerateControllerName($controller)
        {
            return ucfirst($controller) . CONTROLLER_POSTFIX;
        }

        private static function Json($returnValue)
        {
            echo $returnValue;
        }
    }






?>